<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="primus" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="fn" tagdir="/WEB-INF/tags/form" %>
<primus:page title="售后申请列表">
    <jsp:attribute name="css">
        <style type="text/css">
            #table .operate {
                min-width: 80px;
            }
        </style>
    </jsp:attribute>
    <jsp:attribute name="script">
        <script type="application/javascript">
            $(function () {
                $("#table").DataTable({
                    "lengthMenu": [[10, 20, 50, -1], [10, 20, 50, "所有"]],
                    "columnDefs": [{"targets": [7], "orderable": false}],
                    "searching": false,
                    "order": [[0, "asc"]]
                });
            });
            $("body").on("click", ".geo-line", function () {
                var id = $(this).data("id");
            })
        </script>
    </jsp:attribute>
    <jsp:body>
        <div class="breadcrumbs ace-save-state" id="breadcrumbs">
            <ul class="breadcrumb">
                <li>
                    <i class="ace-icon fa fa-home home-icon"></i>
                    <a href="welcome.do">
                        首页
                    </a>
                </li>
                <li>
                    <a href="geo/provinces">
                        订单管理
                    </a>
                </li>
                <li class="active">
                    售后申请列表
                </li>
            </ul>
        </div>

        <div class="page-content">

            <div class="row">
                <div class="col-xs-12">
                    <!-- PAGE CONTENT BEGINS -->
                    <div class="row">
                        <div class="col-xs-12">
                            <h3 class="header smaller lighter blue">
                                售后申请列表
                            </h3>
                            <div class="row">

                                <div class="form-group row">
                                    <label class="col-sm-1 control-label no-padding-right">审核状态</label>
                                    <div class="col-sm-3">
                                        <select id="orderSele">
                                            <option value="begin">未审核</option>
                                            <option value="audited">已审核</option>
                                        </select>
                                    </div>
                                    <label class="col-sm-1 control-label no-padding-right">订单编号</label>
                                    <div class="col-sm-3">
                                        <input type="text" class="col-xs-12 col-sm-12" placeholder="订单编号" id="orderInput">
                                    </div>
                                    <button type="button" class="btn btn-warning ace-icon fa fa-search btn-sm" id="searchButton">
                                        搜索
                                    </button>
                                    <button type="button" class="btn btn-inverse ace-icon fa fa-refresh btn-sm" id="refreshButton">
                                        重置
                                    </button>
                                </div>
                            </div>
                        </div>

                            <div class="row">
                                <table id="table" class="table table-bordered table-striped"
                                       style="width: 100%;">
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <script>
            $(function(){
                // 初始化加载
                first();

                //关闭datatable弹窗提示
                $.fn.dataTable.ext.errMode = 'none'; //不显示任何错误信息

                // 选择行
                $('#table tbody').on('click', 'tr', function () {
                    if ($(this).hasClass('selected')) {
                        if ($(this).hasClass('odd')) {
                            $(this).css({'background-color': '#f9f9f9'})
                        } else {
                            $(this).css({'background-color': '#ffffff'})
                        }
                        $(this).removeClass('selected');
                    } else {
                        if ($("#table").dataTable().$('tr.selected').hasClass('odd')) {
                            $("#table").dataTable().$('tr.selected').css({'background-color': '#f9f9f9'})
                        } else {
                            $("#table").dataTable().$('tr.selected').css({'background-color': '#ffffff'})
                        }
                        $("#table").dataTable().$('tr.selected').removeClass('selected');
                        $(this).addClass('selected');
                        $(this).css({'background-color': '#00c0ef'})
                    }
                });

                // 查看详情(双击行)
                $('#table tbody').on('dblclick', 'tr', function () {
                    var index = $(this).context._DT_RowIndex; //行号
                    var nTrs = $("#table").dataTable().fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                    var row1 = $("#table").dataTable().fnGetData(nTrs[index]);
                    sessionStorage.setItem('aftermarketApplicationsCode', row1.code); // 存入一个值
                    window.location.href="/aftermarketApplications/details";   //跳转详情页面
                });
            });

            // 订单编码检验
            $("#orderInput").blur(function(){
                var inpu = $("#orderInput").val();
                if (inpu != "") {
                    if (inpu.length > 65){
                        layer.msg("对不起，订单编码长度不可超过65")
                    }
                }
            });

            // 订单搜索
            $("#searchButton").on("click", function () {
                $('#table').dataTable().api().ajax.url("/aftermarketApplications/list?state="+$("#orderSele").val()+"&orderCode="+$("#orderInput").val()).load();
            });

            // 重置按钮
            $("#refreshButton").on("click", function () {
                $("#orderSele").val("begin");
                $("#orderInput").val("");
                $('#table').dataTable().api().ajax.reload(null, false);
            })

            // 查看详情(点击'详情'按钮)
            function sendOperation(row) {
                var nTrs = $("#table").dataTable().fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                var row1 = $("#table").dataTable().fnGetData(nTrs[row]);
                sessionStorage.setItem('aftermarketApplicationsCode', row1.code); // 存入一个值
                window.location.href="/aftermarketApplications/details";   //跳转派单页面
            }

            // 表格加载
            function first() {
                $("#table").dataTable({
                    searching: false,
                    ordering: false,
                    bProcessing : true, //开启读取服务器数据时显示正在加载中……特别是大数据量的时候，开启此功能比较好
                    bServerSide : true,//服务器处理分页，默认是false，需要服务器处理，必须true
                    bPaginate : true,//分页工具条显示
                    lengthMenu: [10, 20, 50],
                    ajax: {
                        url: '/aftermarketApplications/list',
                        type: 'GET',
                        dataType: "json",
                        data: {
                            "deliveryType": $("#orderSele").val(),
                            "code": $("#orderInput").val()
                        }
                    },
                    columns: [
                        {
                            "title": "操作",
                        },{
                            "title": "订单来源",
                            "data": "orderSource"
                        },
                        {
                            "title": "订单编号",
                            "data": "orderCode"
                        },
                        {
                            "title": "订单优先级",
                            "data": "deliveryType"
                        },
                        {
                            "title": "售后申请时间",
                            "data": "createTime"
                        },
                        {
                            "title": "售后类型",
                            "data": "expectedReceivingTime"
                        },
                        {
                            "title": "审核状态",
                            "data": "paymentState"
                        },
                        {
                            "title": "退货状态",
                            "data": "returnState"
                        },
                        {
                            "title": "订单状态",
                            "data": "state1"
                        }
                    ],
                    columnDefs: [
                        {
                            "targets": [0],
                            "defaultContent": "",
                            "class": "text-center nowrap",
                            "render": function (data, type, row, full, meta) {
                                return operationFormmater(data, type, row, full, meta);
                            }
                        },
                        {
                            "targets": [1],
                            "defaultContent": "",
                            "class": "text-center nowrap"
                        },
                        {
                            "targets": [2],
                            "defaultContent": "",
                            "class": "text-center nowrap"
                        },
                        {
                            "targets": [3],
                            "defaultContent": "",
                            "class": "text-center nowrap",
                            "render": function (data, type, row, full, meta) {
                                return deliveryTypeFormmater(data);
                            }
                        },
                        {
                            "targets": [4],
                            "defaultContent": "",
                            "class": "text-center nowrap"
                        },
                        {
                            "targets": [5],
                            "defaultContent": "",
                            "class": "text-center nowrap",
                            "render": function (data, type, row) {
                                if (data != undefined) {
                                    return localDateFormmater(data);
                                }
                            }
                        },
                        {
                            "targets": [6],
                            "defaultContent": "",
                            "class": "text-center nowrap"
                        },
                        {
                            "targets": [7],
                            "defaultContent": "",
                            "class": "text-center nowrap",
                        },
                        {
                            "targets": [8],
                            "defaultContent": "",
                            "class": "text-center nowrap"
                        }
                    ],
                });
            }


            // 配送方式格式化
            function deliveryTypeFormmater(data) {
                if ("self_now" == data) {
                    return "自配送-立即送";
                } else if ("self_normal" == data) {
                    return "自配送-普通送";
                } else if ("self_appointment" == data) {
                    return "自配送-预约送";
                } else if ("take_their" == data) {
                    return "到店自提";
                } else if ("thrid_party" == data) {
                    return "第三方物流";
                }
            }

            // 时间格式化
            function localDateFormmater(data) {
                var time = " "+data[3]+":"+data[4]+":"+data[5];
                var day = data[0]+"-"+data[1]+"-"+data[2];
                return day+time;
            }

            // 表格添加'详情'按钮
            function operationFormmater(data, type, row, full, meta) {
                if ($("#orderSele").find("option:selected").text() == "未审核") {
                    return "<button type=\"button\" class=\"btn btn-danger ace-icon fa fa-fire btn-sm\" onclick=\"sendOperation(" + full.row + ")\">\n" +
                        "                                        审核\n" +
                        "                                    </button>";
                } else {
                    return "<button type=\"button\" class=\"btn btn-danger ace-icon fa fa-fire btn-sm\" onclick=\"sendOperation(" + full.row + ")\">\n" +
                        "                                        查看\n" +
                        "                                    </button>";
                }
            }

        </script>
    </jsp:body>
</primus:page>